package com.mmp.main.servlet.device;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDeviceScrappingInfo;
import com.mmp.main.entity.LookupScrappingReason;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class DeviceScrappingProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String deviceId = request.getParameter("deviceId");
		if (deviceId == null || deviceId.isEmpty()) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		HospitalDeviceScrappingInfo scrapping;
		String deviceScrappingId = request.getParameter("deviceScrappingId");
		if (deviceScrappingId != null && !deviceScrappingId.isEmpty())
			scrapping = dbm.findById(Integer.parseInt(deviceScrappingId), HospitalDeviceScrappingInfo.class);
		else
			scrapping = new HospitalDeviceScrappingInfo();
		/*
		 * 
		 * HospitalDevice device = new HospitalDevice();
		 * 
		 * 
		 * String deviceId = request.getParameter("deviceId"); if (deviceId !=
		 * null && !deviceId.isEmpty()) device =
		 * dbm.findById(Integer.parseInt(deviceId), HospitalDevice.class);
		 * 
		 * scrapping.setHospitalDevice(device);
		 * scrapping.setPkId(Integer.parseInt(deviceScrappingId));
		 */

		HospitalDevice device = dbm.findById(Integer.parseInt(deviceId), HospitalDevice.class);
		device.setStatus(0);
		device = dbm.mergeEntity(device);

		scrapping.setHospitalDevice(device);
		scrapping.setDeviceCondition(WebUtil.getParamValue(request, "deviceCondition", null));

		int scrappingReason = WebUtil.getParamValueAsInteger(request, "scrappingReason", 0);
		if (scrappingReason != 0) {
			LookupScrappingReason reason = new LookupScrappingReason();
			reason.setId(scrappingReason);
			scrapping.setLookupScrappingReason(reason);
		}

		String[] scrappingReasonReportUrl = FileUtil.uploadFiles(request, "scrappingReasonReportUrl");
		if (scrappingReasonReportUrl != null && scrappingReasonReportUrl.length != 0)
			scrapping.setScrappingReasonReportUrl(Common.concatenateValues(scrappingReasonReportUrl));


		String[] scrappingFinalReportUrl = FileUtil.uploadFiles(request, "scrappingFinalReportUrl");
		if (scrappingFinalReportUrl != null && scrappingFinalReportUrl.length != 0)
			scrapping.setScrappingFinalReportUrl(Common.concatenateValues(scrappingFinalReportUrl));


		scrapping.setScrappingDate(WebUtil.getParamValueAsDate(request, "scrappingDate", null));

		dbm.mergeEntity(scrapping);

		if (deviceScrappingId == null)
			setMessage(request, new Message("تم التكهين بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		else
			setMessage(request, new Message("تم التعديل بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		sendRedirect(request, response, "SearchForDeviceDisplay?" + Default.SEARCH_DEST_PARAM_NAME + "="
				+ Default.SEARCH_DEST_SCRAPP_DEVICE_PARAM_VALUE);
	}
}
